How can I run .aggregate() on a field introduced using .extra(select={...}) in a Django Query?

Posted by Jake on Stack Overflow See other posts from Stack Overflow or by Jake
Published on 2010-12-31T01:40:42Z Indexed on 2010/12/31 1:54 UTC
Read the original article Hit count: 407

I'm trying to get the count of the number of times a player played each week like this:

player.game_objects.extra(select={'week': 'WEEK(`games_game`.`date`)'}).aggregate(count=Count('week'))

But Django complains that

FieldError: Cannot resolve keyword 'week' into field. Choices are: <lists model fields>

I can do it in raw SQL like this

SELECT WEEK(date) as week, COUNT(WEEK(date)) as count FROM games_game
WHERE player_id = 3
GROUP BY week

Is there a good way to do this without executing raw SQL in Django?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about django